package cn.kgc.tangcco.anyang.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.kgc.tangcco.anyang.pojo.Person;
import cn.kgc.tangcco.anyang.pojo.Student;
import cn.kgc.tangcco.anyang.pojo.User;
import cn.kgc.tangcco.anyang.pojo.Users;

@Controller
@RequestMapping(value = "/user")
@SessionAttributes("message")
public class UserAction {
	public UserAction() {
		System.out.println("轻轻的我来了");
	}

	@RequestMapping(value = "/hello1.action")
	public String hello1(Model model) {
		model.addAttribute("message", "我爱你中国，亲爱的母亲");
		return "/index.jsp";
	}

	@RequestMapping(value = "/hello2.action")
	public String hello2(Model model, HttpSession session) {
		model.addAttribute("message", "我爱你中国，亲爱的母亲");
		session.setAttribute("msg", "课工场,你的职业梦工场!");
		return "/index.jsp";
	}

	@RequestMapping(value = "/hello3.action")
	public String hello2(Model model) {
		model.addAttribute("message", "我爱你中国，亲爱的母亲");
		return "/index.jsp";
	}

	@RequestMapping(value = "/hello4.action")
	public String hello4(Model model) {
		model.addAttribute("message", "我爱你中国，亲爱的母亲");
		// 网页重定向推荐用下面这种方式
		// return "redirect:/index.jsp";
		// return "forward:/WEB-INF/jsp/index.jsp";
		// 网页请求转发推荐用下面这种方式
		// /WEB-INF/jsp/index.jsp
		// return "/index";
		return "/indexjsp";
	}

	@RequestMapping(method = RequestMethod.POST, value = "login1.action")
	public String login1Method(Model model, String account, String password) {
		System.out.println(account);
		System.out.println(password);
		return "/index.jsp";
	}

	@RequestMapping(method = RequestMethod.POST, value = "login2.action")
	public String login2Method(Model model, User user) {
		System.out.println(user.getAccount());
		System.out.println(user.getPassword());
		return "/index.jsp";
	}

	@RequestMapping(method = RequestMethod.POST, value = "login3.action")
	public String login3Method(Model model, Person person) {
		if (person.getAdmin() != null) {
			System.out.println("管理员登录");
			System.out.println("管理员账号 >>> " + person.getAdmin().getAccount());
			System.out.println("管理员密码>>> " + person.getAdmin().getPassword());
		}
		if (person.getEmp() != null) {
			System.out.println("员工登录");
			System.out.println("员工账号 >>> " + person.getEmp().getAccount());
			System.out.println("员工密码>>> " + person.getEmp().getPassword());
		}
		return "/index.jsp";
	}

	/**
	 * 批量删除
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteBySelected")
	public String deleteBySelected(Model model, Integer[] ids) {
		if (ids != null && ids.length > 0) {
			Arrays.stream(ids).forEach(id -> {
				System.out.println(id);
			});
		} else {
			System.out.println("没有选中删除");
		}
		return "/index.jsp";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/addOfBatch")
	public String addOfBatch(Model model, Users users) {
		if (users != null && users.getUserList() != null && users.getUserList().size() > 0) {
			users.getUserList().forEach(user -> {
				System.out.println(user.getAccount() + "\t" + user.getPassword());
			});
			model.addAttribute("message", "员工注册成功");
			System.out.println("员工注册成功");
		} else {
			System.out.println("没有接收到任何数据");
		}
		return "/index.jsp";
	}

	/**
	 * 根据用户ID查找该用户详细信息
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/findById")
	public String findById(Model model, Integer id) {
		System.out.println(id);
		return "/index.jsp";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/login4/{account}/{pwssword}")
	public String login4Method(Model model, @PathVariable("account") String account,
			@PathVariable("pwssword") String password) {
		System.out.println(account);
		System.out.println(password);
		return "/index.jsp";
	}

	/**
	 * string2json
	 */
	@RequestMapping(value = "/string2json")
	@ResponseBody
	public String string2json() {
		return "哈哈";
	}

	/**
	 * bean2json
	 */
	@RequestMapping(value = "/bean2json")
	@ResponseBody
	public Student bean2json() {
		return new Student(1, "李昊哲", new Date());
	}

	/**
	 * bean2json
	 */
	@RequestMapping(value = "/list2json")
	@ResponseBody
	public List<Student> list2json() {
		List<Student> students = new ArrayList<Student>() {{
			for (int i = 0; i < 10; i++) {
				add(new Student(i, "stu" + i, new Date()));
			}
		}};
		return students;
	}
	@RequestMapping(value = "/map2json")
	@ResponseBody
	public Map<String, Object> map2json() throws Exception {
		// 创建List对象
		List<User> userList = new ArrayList<User>();
		// 向List对象中添加三个User对象
		userList.add(new User("1", "system"));
		userList.add(new User("2", "123456"));
		userList.add(new User("3", "system"));
		userList.add(new User("4", "123456"));
		userList.add(new User("5", "system"));
		userList.add(new User("6", "123456"));
		userList.add(new User("7", "system"));
		userList.add(new User("8", "123456"));
		userList.add(new User("9", "system"));
		userList.add(new User("10", "123456"));
		userList.add(new User("11", "system"));
		userList.add(new User("12", "123456"));
		userList.add(new User("13", "system"));
		userList.add(new User("14", "123456"));
		userList.add(new User("15", "system"));
		userList.add(new User("16", "123456"));
		userList.add(new User("17", "system"));
		userList.add(new User("18", "123456"));
		userList.add(new User("19", "system"));
		userList.add(new User("20", "123456"));
		userList.add(new User("21", "system"));
		userList.add(new User("22", "123456"));
		userList.add(new User("23", "system"));
		userList.add(new User("24", "123456"));
		userList.add(new User("25", "system"));
		userList.add(new User("26", "123456"));
		// 创建Map对象
		Map<String, Object> userMap = new LinkedHashMap<String, Object>();
		// 向Map对象中绑定二个变量
		userMap.put("total", userList.size());
		userMap.put("rows", userList);
		// 返回需要转JSON文本的对象
		return userMap;
	}
}
